using System.Net.Sockets;
using System.Text;

try
{
    const int port = 9000;
    var serverIp = "127.0.0.1";

    while (true)
    {
        using var client = new TcpClient(serverIp, port);

        using var stream = client.GetStream();

        var request = "Hello, Server!";
        var requestBytes = Encoding.UTF8.GetBytes(request);


        stream.Write(requestBytes, 0, requestBytes.Length);
        Console.WriteLine($"已发送请求至服务器：{request}");

        var responseBytes = new byte[4096];
        int bytesRead = stream.Read(responseBytes, 0, responseBytes.Length);
        var response = Encoding.UTF8.GetString(responseBytes, 0, bytesRead);
        Console.WriteLine($"接收到来自服务器的响应：{response}");
        Console.ReadKey();
    }
}
catch (Exception ex)
{
    Console.WriteLine($"发生异常：{ex.Message}");
}